package com.jaks.xpense;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.jaks.xpense.data.Incomes;
import com.jaks.xpense.data.entities.Income;
import com.jaks.xpense.data.entities.PaymentType;
import com.jaks.xpense.data.entities.TableEntity;
import com.jaks.xpense.utils.Globals;

public class FilterIncomesActivity extends ListActivity {

	private Incomes incomes;
	private Cursor filteredIncomes;

	private TextView totalView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		filteredIncomes = null;
		incomes = new Incomes(this);
		setContentView(R.layout.filterexpenses);

		totalView = (TextView) findViewById(R.id.searchTotal);
		totalView.setText(Globals.Empty);
	}

	@Override
	protected void onStart() {
		super.onStart();

		if (filteredIncomes == null) {

			filteredIncomes = incomes.readAll();
			startManagingCursor(filteredIncomes);

			populate();
		}
		totalView.setText(Globals.toDecimalText(getTotal()));
	}

	private double getTotal() {
		double total = 0;
		if (filteredIncomes == null)
			return total;

		if ((filteredIncomes.getCount() > 0) && filteredIncomes.moveToFirst()) {

			do {
				double amount = filteredIncomes.getDouble(filteredIncomes.getColumnIndex(Income.Amount));
				total += amount;
			} while (filteredIncomes.moveToNext());
		}

		return total;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		incomes.close();
	}

	private void populate() {

		String[] tableColumns = new String[] { Income.IncomeDate, Income.Amount, Income.Comments,
		    PaymentType.Name };
		int[] columnNames = new int[] { R.id.rowDate, R.id.rowAmount, R.id.rowComments,
		    R.id.rowPaymentType };

		setListAdapter(new SimpleCursorAdapter(this, R.layout.tile_expense_row, filteredIncomes,
		    tableColumns, columnNames) {
			@Override
			public void setViewText(TextView view, String text) {
				super.setViewText(view, formatText(view, text));
			}
		});
	}

	private String formatText(TextView view, String text) {
		switch (view.getId()) {
			case R.id.rowAmount:
				double amount = filteredIncomes.getDouble(filteredIncomes.getColumnIndex(Income.Amount));
				return Globals.toDecimalText(amount);
		}
		return text;
	}

	private void addIncome() {
		Intent intent = new Intent(this, EditIncomeActivity.class);
		startActivityForResult(intent, 0);
	}

	private void editIncome(long id) {
		Intent intent = new Intent(this, EditIncomeActivity.class);
		intent.putExtra(TableEntity.Id, id);
		startActivityForResult(intent, 0);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode != RESULT_OK)
			return;
		filteredIncomes.requery();
		totalView.setText(Globals.toDecimalText(getTotal()));
	}

	@Override
	protected void onListItemClick(ListView listView, View view, int position, long id) {
		super.onListItemClick(listView, view, position, id);

		editIncome(id);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		menu.add(Menu.NONE, R.string.newIncome, Menu.NONE, R.string.newIncome).setIcon(
		    android.R.drawable.ic_menu_add);

		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {

		switch (item.getItemId()) {
			case R.string.newIncome:
				addIncome();
				return true;
		}

		return super.onMenuItemSelected(featureId, item);
	}
}
